package cn.bandeqing.shortMessage.rpc.impl;

import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.base.resp.Table;
import cn.bandeqing.shortMessage.req.ShortMessageTableReq;
import cn.bandeqing.shortMessage.resp.ShortMessageTable;
import cn.bandeqing.shortMessage.rpc.IShortMessageRpc;
import cn.bandeqing.shortMessage.service.IShortMessageService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumnBehavior;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboService;
import org.dromara.hutool.core.text.StrUtil;

import static cn.bandeqing.shortMessage.entity.table.ShortMessageTableDef.SHORT_MESSAGE;

@DubboService
public class ShortMessageRpcImpl implements IShortMessageRpc {

    @Resource
    private IShortMessageService shortMessageService;

    @Override
    public Resp<Table<ShortMessageTable>> table(ShortMessageTableReq req) {
        QueryWrapper qw = QueryWrapper.create()
                .where(SHORT_MESSAGE.SM_ID.eq(req.getSmId()))
                .where(SHORT_MESSAGE.SM_PHONE.eq(req.getSmPhone(), StrUtil::isNotBlank))
                .where(SHORT_MESSAGE.SM_CONTENT.like(req.getSmContent(), StrUtil::isNotBlank))
                .where(SHORT_MESSAGE.SM_PROVIDER_TYPE.like(req.getSmProviderType()))
                .where(SHORT_MESSAGE.SM_CODE.like(req.getSmCode(), StrUtil::isNotBlank))
                .where(SHORT_MESSAGE.SM_ARRIVE.like(req.getSmArrive()))
                .where(SHORT_MESSAGE.SM_CHECK.like(req.getSmCheck()))
                .where(SHORT_MESSAGE.CREATE_TIME.ge(req.getCreateTimeStart()))
                .where(SHORT_MESSAGE.CREATE_TIME.le(req.getCreateTimeEnd()))
                .orderBy(SHORT_MESSAGE.SM_ID.desc());

        Page<ShortMessageTable> page = shortMessageService.pageAs(Page.of(req.getCurrent(), req.getPageSize()), qw, ShortMessageTable.class);
        return Resp.success(Table.of(page.getRecords(), page.getTotalRow()));
    }

}
